package com.breeze.leetcode.easy;

/**
 * @url <a href="https://leetcode.cn/problems/replace-elements-with-greatest-element-on-right-side/">1299. 将每个元素替换为右侧最大元素</a>
 * @author breeze
 * @date 2025/2/16
 */
public class T1299ReplaceElementsWithGreatestElementOnRightSide {
    public static void main(String[] args) {
        int[] arr = {17,18,5,4,6,1};
        T1299ReplaceElementsWithGreatestElementOnRightSide solution = new T1299ReplaceElementsWithGreatestElementOnRightSide();
        int[] result = solution.replaceElements(arr);
        for (int i : result) {
            System.out.println(i);
        }
    }

    public int[] replaceElements(int[] arr) {
        int temp, max = arr[arr.length - 1];
        arr[arr.length - 1] = -1;
        if (arr.length == 1) {
            return arr;
        }

        for (int i = arr.length - 2; i >= 0; i--) {
            temp = arr[i];
            arr[i] = max;
            max = Math.max(max, temp);
        }
        return arr;
    }
}
